﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Exam.Core.DBUtility;
using System.Data;

namespace Exam.Core.SQLDAL
{

    public class Course {


        public List<Exam.Core.Model.Course> getCoursesList(string courseid) {
            string SQL = "SELECT * FROM CourseView WHERE CourseID in (" + courseid.Trim(',') + ") ";
            DataTable dt = DbHelperSQL.Query(SQL).Tables[0];
            List<Exam.Core.Model.Course> list = new List<Exam.Core.Model.Course>();
            foreach (DataRow dr in dt.Rows) {
                Exam.Core.Model.Course obj = new Exam.Core.Model.Course();
                obj.Id = dr["CourseID"].ToString().Trim();
                obj.Title = dr["CourseName"].ToString().Trim();
                obj.Parent = "0";//dr["ParentId"].ToString().Trim();
                //obj.Knowledges = new Exam.Core.SQLDAL.Knowledge().getKnowledgeList(obj.Id);
                obj.KnowCount = dr["KnowNumber"].ToString().Trim();
                obj.QuestionCount = dr["questionNumber"].ToString().Trim();
                list.Add(obj);
            }
            return list;

        }

        public Exam.Core.Model.Course getCourseById(int id) {
            //string path = FilePath.CourseXmlPath.Trim();
            //XmlDocument doc = XmlHelper.GetXmlDocument(path);
            //XmlNodeList nodeList = doc.GetElementsByTagName("course");
            //Exam.Core.Model.Course obj = new Exam.Core.Model.Course();
            //foreach (XmlNode node in nodeList) {
            //    if (id.ToString() == node.Attributes["id"].Value.Trim()) {
            //        obj.Id = id.ToString();
            //        obj.Title = node.Attributes["title"].Value;
            //        obj.Parent = node.Attributes["parent"].Value;
            //        obj.Knowledges = new Exam.Core.SQLDAL.Knowledge().getKnowledgeList(id.ToString());//课程下知识点
            //        obj.QuestionCount = new Exam.Core.SQLDAL.Question().getCountByCourseId(id.ToString());//该课程下试题数目
            //        obj.QuestionTypes = new Exam.Core.SQLDAL.QuestionType().getQuestionTypeListByCourseId(id.ToString());//该课程下题型列表
            //    }

            //}
            //
            string SQL = "SELECT * FROM CourseView WHERE CourseID=" + id;
            DataTable dt = DbHelperSQL.Query(SQL).Tables[0];
            Exam.Core.Model.Course obj = new Exam.Core.Model.Course();
            obj.Id = dt.Rows[0]["CourseID"].ToString().Trim();
            obj.Title = dt.Rows[0]["CourseName"].ToString().Trim();
            obj.Parent = "0";
            obj.Knowledges = new Exam.Core.SQLDAL.Knowledge().getKnowledgeList(obj.Id);
            obj.KnowCount = dt.Rows[0]["KnowNumber"].ToString().Trim();
            obj.QuestionCount = dt.Rows[0]["questionNumber"].ToString().Trim();
            obj.Knowledges = new Exam.Core.SQLDAL.Knowledge().getKnowledgeList(id.ToString());//课程下知识点
            obj.QuestionTypes = new Exam.Core.SQLDAL.QuestionType().getQuestionTypeListByCourseId(id.ToString());//该课程下题型列表
            return obj;
        }

        public bool AddCourse(string cname,string cdesc, string cidlist, string masterid) {
            string SQL = "INSERT INTO Course (CourseName,CourseDesc,ParentId) VALUES ('" + cname + "','" + cdesc + "',0)";
            int rows = DbHelperSQL.ExecuteSql(SQL);
            bool result = false;
            if (rows > 0) {
                //将课程添加到master表中
                int id = DbHelperSQL.GetMaxID("CourseID", "Course");
                string ids = cidlist.Trim(',') + "," + id;
                SQL = "UPDATE Master SET CourseId='" + ids.Trim() + "' WHERE MasterID=" + masterid;
                int a = DbHelperSQL.ExecuteSql(SQL);
                if (a > 0)
                    result = true;
            }
            return result;
        }

        public bool DelCourse(string cid) {
            string path = FilePath.CourseXmlPath.Trim();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("course");
            foreach (XmlNode node in nodeList) {
                string Id = node.Attributes["id"].Value;
                if (Id.Trim() == cid.Trim())
                    node.RemoveAll();
            }
            bool result = false;
            try {
                xmlDoc.Save(path);
                result = true;
            }
            catch (System.Exception ex) {
                result = false;
            }
            finally {
                xmlDoc = null;
            }
            return result;
        }


        public bool EditCourse(string cid,string cname) {
            string path = FilePath.CourseXmlPath.Trim();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("course");
            foreach (XmlNode node in nodeList) {
                string Id = node.Attributes["id"].Value;
                if (Id.Trim() == cid.Trim())
                    node.Attributes["id"].Value = cname;
            }
            bool result = false;
            try {
                xmlDoc.Save(path);
                result = true;
            }
            catch (System.Exception ex) {
                result = false;
            }
            finally {
                xmlDoc = null;
            }
            return result;
        }


        internal List<Model.Course> getCourseList(string p) {
            throw new NotImplementedException();
        }
    }
}
